package com.lplus.apptstprj;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListActivity extends Activity implements OnItemClickListener, View.OnClickListener {

	
	private static final String[] items = { "Lorem", "ipsum", "dolor", "sit",
			"amet", "Abcd", "12344567", "asdfasf bmbmbm" };

	private TextView selection;
	private ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_main);
		
		list = (ListView) findViewById(R.id.list_one);

//		list.setAdapter(new ArrayAdapter<String>(this, R.layout.row_item_1,
//				R.id.row_text_one, items));
		
		ArrayList<RowModel> arrList = new ArrayList<RowModel>();
		for (String s : items) {
			arrList.add(new RowModel(s));
		}
		
		list.setAdapter(new MyNextAdapter(arrList));
		//list.setAdapter(new CustomAdapter());
		list.setOnItemClickListener(this);
		selection = (TextView) findViewById(R.id.selection);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		selection.setText(items[position]);
		
	}
	
	class CustomAdapter extends ArrayAdapter<String> {
		public CustomAdapter() {
			super(ListActivity.this, R.layout.row_item_1, items);
			
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			//convertView is either null or re-cycled view that can be used for new item (data)
			//some inflated view can be stored in a row's tag (for faster access), or Holder object can be stored there (to hold more views, data..)
			LayoutInflater li = getLayoutInflater();
			View row;
			if (items[position].matches("[A-Z].*")) {
				row = li.inflate(R.layout.row_item_headline, parent, false);
			} else {
				row = li.inflate(R.layout.row_item_1, parent, false);
				ImageView img = (ImageView) row.findViewById(R.id.image_one);
				if (position%2 == 0) {
					img.setImageResource(R.drawable.ic_nok_wave);
				} else {
					img.setImageResource(R.drawable.ok_check);
				}
			}
			
			TextView label = (TextView) row.findViewById(R.id.row_text_one);
			label.setText(items[position]);
			return row;
		}
	}

	private RowModel getModel(int position) {
		return ((MyNextAdapter)list.getAdapter()).getItem(position);
	}
	
	class MyNextAdapter extends ArrayAdapter<RowModel> {
		public MyNextAdapter(ArrayList<RowModel> list) {
			super(ListActivity.this, R.layout.row_rating, R.id.row_text_one, list);
		}
		

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = super.getView(position, convertView, parent);
			ViewHolder holder = (ViewHolder) row.getTag();

			if (holder == null) {
				holder = new ViewHolder(row);
				row.setTag(holder);
				RatingBar.OnRatingBarChangeListener l =
					new RatingBar.OnRatingBarChangeListener() {
					public void onRatingChanged(RatingBar ratingBar,
							float rating,
							boolean fromTouch) {
						Integer myPosition = (Integer)ratingBar.getTag();
						RowModel model = getModel(myPosition);
						model.rating = rating;
						RelativeLayout parent = (RelativeLayout)ratingBar.getParent();
						TextView label = (TextView)parent.findViewById(R.id.row_text_one);
						label.setText(model.toString());
						ImageView iv = (ImageView) parent.findViewById(R.id.rate_image_view);
						if (rating>2) {
							iv.setVisibility(View.VISIBLE);
						} else {
							iv.setVisibility(View.GONE);
						}
					}
				};
				row.setOnClickListener(ListActivity.this);
				holder.rate.setOnRatingBarChangeListener(l);
			}
			RowModel model = getModel(position);
			holder.rate.setTag(new Integer(position));
			holder.rate.setRating(model.rating);
			return(row);	
		}
	}
	
	class RowModel {
		String label;
		float rating=2.0f;
		RowModel(String label) {
			this.label=label;
		}
		public String toString() {
			if (rating>=3.0) {
				return(label.toUpperCase());
			}
			return(label);
		}
	}

	
	@Override
	public void onClick(View v) {
		Log.d(toString(), "onClick..");
		int pos = (Integer) v.findViewById(R.id.rate).getTag();
		selection.setText(items[pos]);
	}
}
